<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 渲染成table -->
    <div class="box">
      <!-- alt + z 自动换行 -->
      <!-- 表格标签 -->
      <table border="1" cellspacing="0" cellpadding="10">
        <thead>
          <tr>
            <th>id</th>
            <th>name</th>
            <th>price</th>
          </tr>
        </thead>
        <tbody></tbody>
      </table>
    </div>
    <button>渲染数据</button>
    <!-- 额外需求：手动添加数据，自动追加到table上 -->
    <!-- <input type="text" name="" id="">
    <input type="text" name="" id="">
    <input type="text" name="" id="">
    <button>确定新增</button> -->

    <script>
      let data = [
        {
          id: 1,
          name: 'iPhone15',
          price: 5999,
        },
        {
          id: 2,
          name: 'iPhone15 Pro',
          price: 6999,
        },
        {
          id: 3,
          name: 'iPhone15 Pro Max',
          price: 7999,
        },
      ];
      let tbody = document.querySelector('tbody');
      let btn = document.querySelector('button');
      function render() {
        data.forEach((item) => {
          let tr = document.createElement('tr');
          tbody.appendChild(tr);
          for (let key in item) {
            let td = document.createElement('td');
            td.append(document.createTextNode(item[key]));
            tr.appendChild(td);
          }
        });
      }

      btn.onclick = render;
    </script>
  </body>
</html>
